!
function(e, t) {
	"object" == typeof exports && "object" == typeof module ? module.exports = t(require("Cesium")) : "function" == typeof define &&
		define.amd ? define(["Cesium"], t) : "object" == typeof exports ? exports.CesiumApp = t(require("Cesium")) : e.CesiumApp =
		t(e.Cesium)
}("undefined" != typeof self ? self : this,
	function(cesium) {
		function app(option, callback) {
			var _this = this;
			this.viewer = new Cesium.Viewer(option.domElement, {
				animation: false, //是否创建动画小器件，左下角仪表
				baseLayerPicker: false, //是否显示图层选择器
				fullscreenButton: false, //是否显示全屏按钮
				geocoder: false, //是否显示geocoder小器件，右上角查询按钮
				homeButton: false, //是否显示Home按钮
				infoBox: true, //是否显示信息框
				sceneModePicker: true, //是否显示3D/2D选择器
				selectionIndicator: true, //是否显示选取指示器组件
				timeline: false, //是否显示时间轴
				navigationHelpButton: true, //是否显示右上角的帮助按钮
				scene3DOnly: true, //如果设置为true，则所有几何图形以3D模式绘制以节约GPU资源
				clock: new Cesium.Clock(), //用于控制当前时间的时钟对象
				fullscreenElement: document.body, //全屏时渲染的HTML元素,
				useDefaultRenderLoop: true, //如果需要控制渲染循环，则设为true
				targetFrameRate: undefined, //使用默认render loop时的帧率
				showRenderLoopErrors: false, //如果设为true，将在一个HTML面板中显示错误信息
				automaticallyTrackDataSourceClocks: true, //自动追踪最近添加的数据源的时钟设置
				contextOptions: undefined, //传递给Scene对象的上下文参数（scene.options）
				sceneMode: Cesium.SceneMode.SCENE3D, //初始场景模式
				mapProjection: new Cesium.WebMercatorProjection(), //地图投影体系
				dataSources: new Cesium.DataSourceCollection()
			});

			this.scene = this.viewer.scene;
			this.canvas = this.viewer.canvas;
			this.clock = this.viewer.clock;
			this.camera = this.viewer.scene.camera;
			this.entities = this.viewer.entities;
			this.primitives = this.viewer.primitives;
			this.ellipsoid = this.scene.globe.ellipsoid;
			this.handler = new Cesium.ScreenSpaceEventHandler(this.scene.canvas);

			this.viewer.scene.screenSpaceCameraController.minimumZoomDistance = 100; //相机的高度的最小值
			// this.viewer.scene.screenSpaceCameraController.maximumZoomDistance = 20000000; //相机高度的最大值
			// this.viewer.scene.screenSpaceCameraController._minimumZoomRate = 30000; // 设置相机缩小时的速率
			// this.viewer.scene.screenSpaceCameraController._maximumZoomRate = 5906376272000 //设置相机放大时的速率

			var startMousePosition;
			var mousePosition;
			this.handler.setInputAction(function(movement) {
				mousePosition = startMousePosition = Cesium.Cartesian3.clone(movement.position);
				_this.handler.setInputAction(function(movement) {
					mousePosition = movement.endPosition;
					var y = mousePosition.y - startMousePosition.y;
					if (y > 0) {
						_this.scene.screenSpaceCameraController.enableTilt = true;
					}
				}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
			}, Cesium.ScreenSpaceEventType.MIDDLE_DOWN);


			//移动设备上禁掉以下几个选项，可以相对更加流畅
			if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
				this.viewer.scene.fog.enable = false;
				this.viewer.scene.skyAtmosphere.show = false;
				this.viewer.scene.fxaa = false;
			}
			this.cn();
			this.hide();
			if (option.homePostion) {
				this.flyToByPosition(option.homePostion, 0, function(e) {

				});
				Cesium.Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees(89.5, 20.4, 110.4, 61.2); //homebutton默认跳转位置
			}
			if (callback) callback(this);

			// 设置鼠标位置经纬度\视角高度实时显示
			var longitude_show = document.getElementById('longitude_show');
			var latitude_show = document.getElementById('latitude_show');
			var altitude_show = document.getElementById('altitude_show');
			var electude_show = document.getElementById('electude_show');
			//鼠标移动
			this.handler.setInputAction(function(event) {
				try {
					var cartesian = _this.camera.pickEllipsoid(event.endPosition, _this.ellipsoid);
					if (cartesian) {
						var cartographic =_this. ellipsoid.cartesianToCartographic(cartesian);
						var lat_String = Cesium.Math.toDegrees(cartographic.latitude).toFixed(4);//经度
						var log_String = Cesium.Math.toDegrees(cartographic.longitude).toFixed(4);//纬度
						var alti_String = (_this.camera.positionCartographic.height / 1000).toFixed(2);//视角高度
						var elec_String =0;
						if((_this.scene.globe.getHeight(cartographic))){
							elec_String = (_this.scene.globe.getHeight(cartographic)).toFixed(4);//海拔高度
						}
						longitude_show.innerHTML = log_String;
						latitude_show.innerHTML = lat_String;
						altitude_show.innerHTML = alti_String;
						electude_show.innerHTML = elec_String;
					}
				} catch (e) {
					console.log(e);
				}
			}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
			
			
			//键盘事件ctrl+shift+D切换模板操作
			document.addEventListener('keydown', function(event) {
				var e = event || window.event || arguments.callee.caller.arguments[0];
				if (e) {
					var keyCode = event.keyCode || event.which || e.charCode;
					if (event.ctrlKey == true && keyCode == 70) {
						_this.scene.debugShowFramesPerSecond = !_this.scene.debugShowFramesPerSecond;
						event.preventDefault();
					}
				}
			});
			
			
		}
		cesium.defineProperties(app.prototype, {
			DEFAULT_VIEW_RECTANGLE: {
				get: function() {
					return this._DEFAULT_VIEW_RECTANGLE
				},
				set: function(e) {
					return _DEFAULT_VIEW_RECTANGLE = e
				}
			},
		})
		/**
		 * 隐藏logo以及地图服务版权信息
		 */
		app.prototype.hide = function() {
			var viewer = this.viewer;
			viewer._cesiumWidget._creditContainer.style.display = "none";
			viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
		}
		/**
		 * 汉化帮助按钮
		 */
		app.prototype.cn = function() {
			var _this = this;
			var viewer = this.viewer;
			if (viewer.navigationHelpButton) {
				viewer.navigationHelpButton.viewModel.tooltip = "操作指南";
				var clickHelper = viewer.navigationHelpButton.container.getElementsByClassName("cesium-click-navigation-help")[0];
				var touchHelper = viewer.navigationHelpButton.container.getElementsByClassName("cesium-touch-navigation-help")[0];

				var button = viewer.navigationHelpButton.container.getElementsByClassName("cesium-navigation-button-right")[0]
				button.innerHTML = button.innerHTML.replace(">Touch", ">手势");
				button = viewer.navigationHelpButton.container.getElementsByClassName("cesium-navigation-button-left")[0]
				button.innerHTML = button.innerHTML.replace(">Mouse", ">鼠标");

				var click_help_pan = clickHelper.getElementsByClassName("cesium-navigation-help-pan")[0];
				click_help_pan.innerHTML = "平移";
				var click_help_pan_details = click_help_pan.parentNode.getElementsByClassName("cesium-navigation-help-details")[0];
				click_help_pan_details.innerHTML = "按下左键 + 拖动";

				var click_help_zoom = clickHelper.getElementsByClassName("cesium-navigation-help-zoom")[0];
				click_help_zoom.innerHTML = "旋转";
				click_help_zoom.parentNode.getElementsByClassName("cesium-navigation-help-details")[0].innerHTML = "按下右键+拖动";
				click_help_zoom.parentNode.getElementsByClassName("cesium-navigation-help-details")[1].innerHTML = "";

				var click_help_rotate = clickHelper.getElementsByClassName("cesium-navigation-help-rotate")[0];
				click_help_rotate.innerHTML = "缩放";
				click_help_rotate.parentNode.getElementsByClassName("cesium-navigation-help-details")[0].innerHTML = "滚动鼠标滚轮";
				click_help_rotate.parentNode.getElementsByClassName("cesium-navigation-help-details")[1].innerHTML = "";

				//触屏操作
				var touch_help_pan = touchHelper.getElementsByClassName("cesium-navigation-help-pan")[0];
				touch_help_pan.innerHTML = "平移";
				touch_help_pan.parentNode.getElementsByClassName("cesium-navigation-help-details")[0].innerHTML = "单指拖动";

				var touch_help_zoom = touchHelper.getElementsByClassName("cesium-navigation-help-zoom")[0];
				touch_help_zoom.innerHTML = "缩放";
				touch_help_zoom.parentNode.getElementsByClassName("cesium-navigation-help-details")[0].innerHTML = "双指捏合";

				var touch_help_tilt = touchHelper.getElementsByClassName("cesium-navigation-help-rotate")[0];
				touch_help_tilt.innerHTML = "俯仰";
				touch_help_tilt.parentNode.getElementsByClassName("cesium-navigation-help-details")[0].innerHTML = "双指同向拖动";

				var touch_help_rotate = touchHelper.getElementsByClassName("cesium-navigation-help-tilt")[0];
				touch_help_rotate.innerHTML = "旋转";
				touch_help_rotate.parentNode.getElementsByClassName("cesium-navigation-help-details")[0].innerHTML = "双指反向拖动";
			}
		}
		/**
		 * 生成uuid
		 */
		app.prototype.uuid = function() {
			var s = [];
			var hexDigits = "0123456789abcdef";
			for (var i = 0; i < 36; i++) {
				s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
			}
			s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
			s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
			s[8] = s[13] = s[18] = s[23] = "-";
			var uuid = s.join("");
			return uuid;
		}
		
		
		/**
		 * 飞到指定位置
		 * @param {Object} position 位置
		 * @param {Object} duration 持续动画
		 * @param {Object} callback 回調函數
		 */
		app.prototype.flyToByPosition = function(position, duration, callback) {
			var _this = this;
			_this.camera.flyTo({
				destination: Cesium.Cartesian3.fromDegrees(position[0] * 1, position[1] * 1, position[2] * 1),
				orientation: {
					heading: Cesium.Math.toRadians(-15),
					pitch: Cesium.Math.toRadians(-65),
					roll: Cesium.Math.toRadians(0)
				},
				duration: duration ? duration : 0,
				complete: function() {
					if (callback) callback(_this);
				}
			});
		}
		
		app.prototype.remove=function(model){
			var _this = this;
			_this.scene.primitives.remove(model);
		}
		
		app.prototype.getById=function(id){
			var _this = this;
			return _this.viewer.entities.getById(id);
		}
		cesium.App = app;
	})
